package com.yanqu.road.server.manger.activity;

import com.yanqu.road.entity.activity.ActivityDisperseGoldInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.holiday.ActivityCatMerge;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.holiday.HolidayActivityModule;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.string.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class HolidayActivityMgr extends TempMgr {

    public static final int CONDITION_TYPE_PROGRESS = eGamePlayerEventType.HolidayActivityProgress.getValue();

    private static Logger logger = LogManager.getLogger(HolidayActivityMgr.class.getName());

    //activityId   level
    private static Map<Integer, Map<Integer, ActivityCatMerge>> activityCatMergeMap = new ConcurrentHashMap<>();


    private static RandomHelper random = new RandomHelper();

    @Override
    public boolean reloadData() throws Exception {
        activityCatMergeMap = new ConcurrentHashMap<>();
        return true;
    }

    public static void reloadActivityInfo(){
        logger.info("reload holiday activity start");
        List<ActivityInfo>  activityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.CrossHolidayActivity.getValue());
        if(activityInfoList != null && activityInfoList.size() > 0) {
            List<Integer> activityIdList = new ArrayList<>();
            for (ActivityInfo activityInfo : activityInfoList) {
                activityIdList.add(activityInfo.getActivityId());
            }
            activityCatMergeMap = ActivityBussiness.getActivityCatMergeInfoMap(activityIdList);
            playerReloadActivity();
        }
        logger.info("reload holiday activity end");
    }

    private static void playerReloadActivity() {
        List<GamePlayer> playerList = GamePlayerMgr.getCopyPlayerList();
        for(GamePlayer onlinePlay : playerList){
            onlinePlay.getModule(HolidayActivityModule.class).syncActivity();
        }
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    @Override
    public boolean save() {
        return true;
    }

    /**
     * 随机招募妖灵
     * @param activityId
     * @return
     */
    public static int getRandomCatsLevel(int activityId) {
        Map<Integer, ActivityCatMerge> catsInfoMap = activityCatMergeMap.get(activityId);
        if (catsInfoMap != null) {
            int weight = 0;
            for (ActivityCatMerge catsInfo : catsInfoMap.values()) {
                weight += catsInfo.getWeight();
            }
            int random = new RandomHelper().next(weight);
            weight = 0;
            for (ActivityCatMerge holidayCatsInfo : catsInfoMap.values()) {
                weight += holidayCatsInfo.getWeight();
                if (weight >= random) {
                   return holidayCatsInfo.getLevel();
                }
            }
        }
        return 0;
    }

    public static ActivityCatMerge getCatsInfoByActivityIdAndLevel(int activityId, int level) {
        return activityCatMergeMap.get(activityId).get(level);
    }

    public static Map<Integer, ActivityCatMerge> getCatsInfoByActivityId(int activityId) {
        return activityCatMergeMap.get(activityId);
    }

    public static Map<Integer, Map<Integer, ActivityCatMerge>> getActivityCatMergeMap() {
        return activityCatMergeMap;
    }

    /**
     * 需达到的积分数|达到的南瓜数|银两系数1|银两系数2|糖果系数1|糖果系数2
     * @param activityInfo
     * @return
     */
    public static List<String> getActivityParam3(ActivityInfo activityInfo){
        return Arrays.asList(activityInfo.getParam3().split("\\|"));
    }

    /**
     * 招募令id|积分id|糖果id
     * @param activityInfo
     * @return
     */
    public static List<Integer> getActivityParam4(ActivityInfo activityInfo){
        return StringUtils.stringToIntegerList(activityInfo.getParam4(), "\\|");
    }

    /**
     * 宣言|美女对话1|美女对话2|...
     * @param activityInfo
     * @return
     */
    public static List<String> getActivityParam5(ActivityInfo activityInfo){
        return StringUtils.stringToStringList(activityInfo.getParam5(), "\\|");
    }



    /**
     * 领取送糖奖励
     * @param disperseCandyInfo
     * @param earnSpeed  领取奖励的玩家赚速
     * @return
     */
    public static Property getDisperseGoldReward(ActivityDisperseGoldInfo disperseCandyInfo, ActivityInfo activityInfo, BigInteger earnSpeed) {
        List<String> param3List = getActivityParam3(activityInfo);
        List<Integer> param4List = getActivityParam4(activityInfo);
        //银两奖励
        int totalSilver = random.next(Integer.parseInt(param3List.get(2)), Integer.parseInt(param3List.get(3)));
        totalSilver = totalSilver > GameConfig.DISPERSE_GOLD_MAX_SILVER ? GameConfig.DISPERSE_GOLD_MAX_SILVER : totalSilver;
        BigInteger silver = earnSpeed.multiply(BigInteger.valueOf(totalSilver));

        //随机糖果奖励
        int candyNum = random.next(Integer.parseInt(param3List.get(4)), Integer.parseInt(param3List.get(5)));
        int candyGoodId = param4List.get(2);

        Property property = new Property();
        property.addProperty(GameConfig.GAME_MONEY_SILVER, silver);
        property.addProperty(candyGoodId, BigInteger.valueOf(candyNum));
        return property;
    }

}
